package com.buba.springcloud.controller;

import com.buba.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author 张宇飞
 * @Description:
 * @date 2021/8/917:17
 */
@RestController
public class DeptConsumerController {

    // 消费者不应该有Service层，使用RestTemplate调用Service。RestTemplate封装了多种访问Http请求的类。
    private RestTemplate restTemplate;

    // Ribbon,这里的地址应该是个变量。我们应该通过Eureka中的服务名访问
    // private static final String REST_URL_PREFIX = "http://localhost:8001";
    private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-DEPT";


    @RequestMapping("/consumer/dept/get/{deptId}")
    public Dept get(@PathVariable("deptId") long deptId) {
        // 传递的参数可以使用map进行封装、可以直接在路径上拼接、可以使用实体类传递
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/", Dept.class, deptId);
    }

    @RequestMapping("/consumer/dept/add")
    public boolean add(Dept dept) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/dept/addDept", dept, Boolean.class);
    }

    @RequestMapping("/consumer/dept/list")
    public List<Dept> list() {
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
    }


    @Autowired
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }


}
